package cn.chenli.job.controller;


import cc.tuhaolicai.mt.predefined.database.PageInfo;
import cn.chenli.job.annotation.Mapping;
import cn.chenli.job.annotation.ReturnJson;
import cn.chenli.job.common.JsonResult;
import cn.chenli.job.common.ToolModel;
import cn.chenli.job.constant.Constant;
import cn.chenli.job.model.QuartzJob;
import cn.chenli.job.service.QuartzJobService;
import cn.chenli.job.service.impl.QuartzJobServiceImpl;
import org.asyou.mongo.Page;
import pro.tools.data.text.ToolStr;

@Mapping(value = "/quartz",viewPath = "/WEB-INF/")
public class QuartzJobController extends BaseController {

    private QuartzJobService quartzJobService = new QuartzJobServiceImpl();

    public void index(){
        returnView("view/quartz/quartzJobs.jsp");
    }

    @ReturnJson
    public void list() {
        //接收查询条件
        QuartzJob quartzJob = new QuartzJob();
        //构建分页条件
        PageInfo pageInfo = getPageInfo();

        Page<QuartzJob> page = quartzJobService.findPage(quartzJob, pageInfo);
        //需要测试内部对象是否正常---toolJson转换
        returnJson(page);
    }

    public void savePage(){
        QuartzJob quartzJob = new QuartzJob();
        String id = getParam("id");//获取是否有id传入
        if (!ToolStr.isBlank(id)) {
            quartzJob.setId(id);
            quartzJob = quartzJobService.findOne(quartzJob);//查询对象
            setAttr("saveQuartzJob", quartzJob);
        }else{
            setAttr("defaultClass", Constant.DEFAULT_CLASS);//表示添加则给默认的格式
        }
        returnView("view/quartz/saveQuartzJob.jsp");
    }

    /**
     * 保存修改定时器
     */
    public void save() {
        JsonResult<String> result = new JsonResult<>();
        String name = getParam("name");//名称
        String cron = getParam("cron");//时间表达式
        String script = getParam("script");//执行脚本
        String id = getParam("id");//主键
        if(ToolStr.isBlank(name) || ToolStr.isBlank(cron) || ToolStr.isBlank(script)){
            result.setHaveError(true);
            result.setErrMsg("不能为空");
            returnJson(result);
            return;
        }
        QuartzJob planTaskModel = new QuartzJob();
        planTaskModel.setName(name);
        planTaskModel.setCron(cron);
        planTaskModel.setScript(script);
        boolean resullt = true;
        if(!ToolStr.isBlank(id)){
            resullt = quartzJobService.update(new QuartzJob().setId(id),planTaskModel);
        }else {
            planTaskModel.setId(ToolModel.getId());
            planTaskModel.setStatus(1);
            planTaskModel.setEnabled(Boolean.FALSE);
            planTaskModel = quartzJobService.save(planTaskModel);//保存对象
        }

        if (planTaskModel == null || !resullt) {
            result.setHaveError(true);
            result.setErrMsg("保存失败");
            returnJson(result);
            return;
        }
        result.setHaveError(false);
        result.setData("保存成功");
        returnJson(result);
    }

    /**
     * 修改定时器开关状态，即：开关的开启关闭
     */
    public void updateStatus(){
        JsonResult<String> result = new JsonResult<>();
        QuartzJob planTaskModel = new QuartzJob();//获取保存对象
        String id = getParam("id");
        if (ToolStr.isBlank(id)) {
            result.setHaveError(true);
            result.setErrMsg("参数错误");
            returnJson(result);
            return;
        }
        String enabled = getParam("enabled");

        QuartzJob findQuartzJob = quartzJobService.findOne(new QuartzJob().setId(id));
        if(findQuartzJob == null){
            result.setHaveError(true);
            result.setErrMsg("没有该定时器");
            returnJson(result);
            return;
        }
        if (ToolStr.isBlank(enabled) || !Boolean.parseBoolean(enabled)) {
            planTaskModel.setEnabled(Boolean.FALSE);
            //停止定时器
            boolean resultEnable = quartzJobService.stopJob(findQuartzJob);
            if(!resultEnable) {
                result.setErrMsg("保存失败");
                result.setHaveError(true);
                returnJson(result);
                return;
            }
        }else {
            planTaskModel.setEnabled(Boolean.TRUE);
            boolean resultEnable = quartzJobService.enable(findQuartzJob);
            if(!resultEnable) {
                result.setErrMsg("保存失败");
                result.setHaveError(true);
                returnJson(result);
                return;
            }
        }
        boolean updateResult = quartzJobService.update(new QuartzJob().setId(id),planTaskModel);//保存对象
        if (!updateResult) {
            result.setHaveError(true);
            result.setErrMsg("保存失败");
            returnJson(result);
            return;
        }
        result.setHaveError(false);
        result.setData("保存成功");
        returnJson(result);
    }

    public void deleteById(){
        JsonResult<String> result = new JsonResult<>();
        String idStr = getParam("id");
        if(ToolStr.isBlank(idStr)){
            result.setHaveError(true);
            result.setErrMsg("删除对象不能为空");
            returnJson(result);
            return;
        }
        String[] ids = idStr.split(",");
        for(String id : ids){
            quartzJobService.delete(new QuartzJob().setId(id));
        }
        result.setHaveError(false);
        result.setData("删除成功");
        returnJson(result);
    }

}
